package com.chinatgl.latte.ec.adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.mode.Cart;


import java.util.List;

/**
 * Created by Administrator on 2016/10/14.
 */

public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartAdapter.MyViewHolder> {


    private FragmentActivity activity;
    private List<Cart.ListBean> data;

    private OnDeleteClickListener mOnDeleteClickListener;
    private OnEditClickListener mOnEditClickListener;
    private OnResfreshListener mOnResfreshListener;

    public ShopCartAdapter(FragmentActivity activity, List<Cart.ListBean> data) {
        this.activity = activity;
        this.data = data;
    }

    @Override
    public ShopCartAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(activity).inflate(R.layout.item_shopcart, parent, false);
        return new ShopCartAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ShopCartAdapter.MyViewHolder holder, final int position) {


        if (position > 0) {
            if (data.get(position).getShopId().equals(data.get(position - 1).getShopId())) {
                Glide.with(activity).load(data.get(position).getSkuImage()).into(holder.ivShopCartClothPic);
                holder.llShopCartHeader.setVisibility(View.GONE);
            } else {
                holder.llShopCartHeader.setVisibility(View.VISIBLE);
                Glide.with(activity).load(data.get(position).getSkuImage()).into(holder.ivShopCartClothPic);
            }
        } else {
            Glide.with(activity).load(data.get(position).getSkuImage()).into(holder.ivShopCartClothPic);
            holder.llShopCartHeader.setVisibility(View.VISIBLE);
        }

        holder.tvShopCartClothColor.setText("规格: " + data.get(position).getProductSpec());
        //属性显示在第一个上面 这里显示的控件暂时先隐藏了

        //商品的名称
        holder.tvShopCartClothName.setText(data.get(position).getProductName());
        //店铺的名称
        holder.tvShopCartShopName.setText(data.get(position).getShopName());
        holder.tv_item_shopcart_cloth_price_fanli.setText("返利" + data.get(position).getRebate() + "元");
        String shopLogo = data.get(position).getShopLogo();
        Glide.with(activity)
                .load(shopLogo + "?imageView2/q/50")
                .placeholder(R.mipmap.logo_tgl)
                .error(R.mipmap.logo_tgl)
                .into(holder.ivShopImg);

        //商品的价格
        holder.tvShopCartClothPrice.setText("¥" + data.get(position).getNowPrice());
        //商品的数量
        holder.etShopCartClothNum.setText(data.get(position).getNum() + "");
//如果监听的接口不是空的  获取商品是不是被选择状态
        if (mOnResfreshListener != null) {
            boolean isSelect = false;
            for (int i = 0; i < data.size(); i++) {
                if (!data.get(i).isSelect()) {
                    isSelect = false;
                    break;
                } else {
                    isSelect = true;
                }
            }
            //条用刷新的方法 刷新数据
            mOnResfreshListener.onResfresh(isSelect);
        }
//编辑单个商品的数量
        holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).getNum() > 1) { //如果比1大可以修改
                    int count = data.get(position).getNum() - 1;
                    if (mOnEditClickListener != null) {
                        mOnEditClickListener.onEditClick(position, data.get(position).getCartItemId(), count);
                    }
//                    //网络删除数量 POST /mobi/cart/edit
//                    // TODO: 2017/5/8  直接设置修改后的数量
//                    data.get(position).setNum(count);
//                    notifyDataSetChanged();
                } else {
                    //小于1 直接是删除了
                    // TODO: 2017/8/11 0011  
                 //   showDialog(v, position);
                }
            }
        });
//
        holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = data.get(position).getNum() + 1;
                if (mOnEditClickListener != null) {
//                    System.out.println("购物车iddata.get(position).getCartItemId()===" + data.get(position).getCartItemId());
                    mOnEditClickListener.onEditClick(position, data.get(position).getCartItemId(), count);
                }
//                //网络删除数量 POST /mobi/cart/edit
//                // TODO: 2017/5/8   直接设置修改后的数量
//                data.get(position).setNum(count);
//                notifyDataSetChanged();
            }
        });

        if (data.get(position).isSelect()) {
            holder.ivShopCartClothSel.setImageDrawable(activity.getResources().getDrawable(R.drawable.shopcart_selected));
        } else {
            holder.ivShopCartClothSel.setImageDrawable(activity.getResources().getDrawable(R.drawable.shopcart_unselected));
        }

        if (data.get(position).isShopSelect()) {
            holder.ivShopCartShopSel.setImageDrawable(activity.getResources().getDrawable(R.drawable.shopcart_selected));
        } else {
            holder.ivShopCartShopSel.setImageDrawable(activity.getResources().getDrawable(R.drawable.shopcart_unselected));
        }

        holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/8/11 0011  
             //   showDialog(v, position);
            }
        });

        holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.get(position).setSelect(!data.get(position).isSelect());
                //通过循环找出不同商铺的第一个商品的位置
                for (int i = 0; i < data.size(); i++) {
                 if (data.get(i).getIsFirst() == 1) {
                        //遍历去找出同一家商铺的所有商品的勾选情况
                        for (int j = 0; j < data.size(); j++) {
                            //如果是同一家商铺的商品，并且其中一个商品是未选中，那么商铺的全选勾选取消
                            if (data.get(j).getShopId() .equals(data.get(i).getShopId())  && !data.get(j).isSelect()) {
                                data.get(i).setShopSelect(false);
                                break;
                            } else {
                                //如果是同一家商铺的商品，并且所有商品是选中，那么商铺的选中全选勾选
                                data.get(i).setShopSelect(true);
                            }
                        }
                  }
                }
                notifyDataSetChanged();
            }
        });
 // setIsFirst //iv_item_shopcart_shopselect
        holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           if (data.get(position).getIsFirst() == 1) {
                    data.get(position).setShopSelect(!data.get(position).isShopSelect());
                    for (int i = 0; i < data.size(); i++) {

                        if (data.get(i).getShopId() .equals(data.get(position).getShopId()) ) {
                            data.get(i).setSelect(data.get(position).isShopSelect());

                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });

    }

//    private void showDialog(final View view, final int position) {
//
//        int[] padiing = new int[]{100, 0, 100, 30};
//        new SuperDialog.Builder(activity)
//                .setRadius(40)
//                .setAlpha(1.0f)
//                .setWidth(0.7f)
//                .setItemsBottomMargin(0)
//                .setTitle("提示")
//                .setMessage("确认删除吗?", Color.parseColor("#666666"), 50, padiing)
//                .setCanceledOnTouchOutside(true)
//                .setDimEnabled(true)
//                .setNegativeButton("狠心删除", Color.parseColor("#333333"), new SuperDialog.OnClickNegativeListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (mOnDeleteClickListener != null) {
//                            mOnDeleteClickListener.onDeleteClick(view, position, data.get(position).getCartItemId());
//                        }
//                    }
//                })
//                .setPositiveButton("点错了", Color.parseColor("#333333"), new SuperDialog.OnClickPositiveListener() {
//                    @Override
//                    public void onClick(View v) {
//
//                    }
//                })
//                .build();
////        //删除商品 // TODO: 2017/5/8  POST /mobi/cart/del
////
////        data.remove(position);
////        //重新排序，标记所有商品不同商铺第一个的商品位置
////       CartFragment.isSelectFirst(data);
////        notifyDataSetChanged();
//    }

    @Override
    public int getItemCount() {
        int count = (data == null ? 0 : data.size());

        return count;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivShopCartShopSel;
        private TextView tvShopCartShopName;
        private TextView tvShopCartClothName;
        private TextView tvShopCartClothPrice;
        private TextView etShopCartClothNum;
        private TextView tvShopCartClothColor;
        private TextView tv_item_shopcart_cloth_price_fanli;

        private ImageView ivShopCartClothSel;
        private ImageView ivShopCartClothMinus;
        private ImageView ivShopCartClothAdd;
        private ImageView ivShopCartClothDelete;
        private ImageView ivShopImg;
        private ImageView ivShopCartClothPic;
        private LinearLayout llShopCartHeader;

        public MyViewHolder(View view) {
            super(view);
            llShopCartHeader = (LinearLayout) view.findViewById(R.id.ll_shopcart_header);
            ivShopImg = (ImageView) view.findViewById(R.id.iv_shop_img);
            ivShopCartShopSel = (ImageView) view.findViewById(R.id.iv_item_shopcart_shopselect);
            tvShopCartShopName = (TextView) view.findViewById(R.id.tv_item_shopcart_shopname);
            tvShopCartClothName = (TextView) view.findViewById(R.id.tv_item_shopcart_clothname);
            tvShopCartClothPrice = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_price);
            etShopCartClothNum = (TextView) view.findViewById(R.id.et_item_shopcart_cloth_num);
            tvShopCartClothColor = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_color);
            tv_item_shopcart_cloth_price_fanli = (TextView) view.findViewById(R.id.tv_item_shopcart_cloth_price_fanli);
//tv_item_shopcart_cloth_price_fanli
            ivShopCartClothSel = (ImageView) view.findViewById(R.id.tv_item_shopcart_clothselect);
            ivShopCartClothMinus = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_minus);
            ivShopCartClothAdd = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_add);
            ivShopCartClothPic = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_pic);
            ivShopCartClothDelete = (ImageView) view.findViewById(R.id.iv_item_shopcart_cloth_delete);
        }
    }


    private ShopCartAdapter.OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(ShopCartAdapter.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(View view, int position, String cartid);
    }

    public void setOnDeleteClickListener(OnDeleteClickListener mOnDeleteClickListener) {
        this.mOnDeleteClickListener = mOnDeleteClickListener;
    }

    public interface OnEditClickListener {
        void onEditClick(int position, String cartid, int count);
    }

    public void setOnEditClickListener(OnEditClickListener mOnEditClickListener) {
        this.mOnEditClickListener = mOnEditClickListener;
    }

    public interface OnResfreshListener {
        void onResfresh(boolean isSelect);
    }

    public void setResfreshListener(OnResfreshListener mOnResfreshListener) {
        this.mOnResfreshListener = mOnResfreshListener;
    }

}
